package servlet;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UserDAO;
import dao.UserDaoJdbcImpl;
import dao.UserInfoDAO;
import dao.UserInfoDaoJdbcImpl;
import modle.userInfo;
import modle.users;
import util.DateChange;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//获取JSON的数据
		String UStuNum = request.getParameter("credit");
		String Username = request.getParameter("uname");
		String password = request.getParameter("password");
		String Uname = request.getParameter("name");
		String Uid = request.getParameter("ID");
		String USex = request.getParameter("options");
		Date a = new Date(0, 0, 0);
		String UData = DateChange.sql_dateToString(a);
		String UMail = request.getParameter("email");
		String UPhone = request.getParameter("tel");
		int UStatus = 0;	//未激活，等待管理员审核
		
		//写入账号密码
		users user = new users(Username,password);	//学号密码一样
		UserDAO ud = new UserDaoJdbcImpl();
		ud.writeUser(user);
		//写入个人信息
		userInfo ui = new userInfo(UStuNum, Username, Uname, Uid, USex, UData, UMail, UPhone, UStatus);
		UserInfoDAO uid = new UserInfoDaoJdbcImpl();
		uid.writeUserInfo(ui);
		
		response.sendRedirect("user_page/Login.jsp");
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
